package com.kangdi.riggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kangdi.riggie.common.Result;
import com.kangdi.riggie.dto.SetmealDto;
import com.kangdi.riggie.entity.Category;
import com.kangdi.riggie.entity.Setmeal;
import com.kangdi.riggie.entity.SetmealDish;
import com.kangdi.riggie.service.CategoryService;
import com.kangdi.riggie.service.SetmealDishService;
import com.kangdi.riggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;
    @PostMapping
    public Result<String> save(@RequestBody  SetmealDto setmealDto){
        System.out.println(setmealDto);
        setmealService.add(setmealDto);
        return Result.success("添加成功!");
    }
    @GetMapping("page")
    public Result<Page> page(int page,int pageSize,String name){
        Page<Setmeal> setmealPage = new Page<>();
        Page<SetmealDto> setmealDtoPage = new Page<>();
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name!=null,Setmeal::getName,name);
        setmealService.page(setmealPage,queryWrapper);
        BeanUtils.copyProperties(setmealPage,setmealDtoPage,"records");
        List<Setmeal> records = setmealPage.getRecords();
        List<SetmealDto> list=records.stream().map((item)->{
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto);
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if (category!=null){
                setmealDto.setCategoryName(category.getName());
            }
            return setmealDto;
        }).collect(Collectors.toList());
        setmealDtoPage.setRecords(list);
        System.out.println(list);
        return Result.success(setmealDtoPage);
    }
    @GetMapping("{id}")
    public Result<SetmealDto> get(@PathVariable long id){
        Setmeal setmeal = setmealService.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(queryWrapper);
        setmealDto.setSetmealDishes(list);
        return Result.success(setmealDto);
    }
    @PutMapping
    public Result<String> update(@RequestBody SetmealDto setmealDto){
        setmealService.updateWithOther(setmealDto);
        return Result.success("修改成功");
    }
    @PostMapping("status/{a}")
    public Result<String> updateStatus(@PathVariable int a,@RequestParam List<Long> ids){
        for (Long id : ids) {
            Setmeal setmeal = new Setmeal();
            setmeal.setId(id);
            setmeal.setStatus(a);
            setmealService.updateById(setmeal);
        }
        return Result.success("状态修改成功");
    }
    @DeleteMapping
    public Result<String> delete(@RequestParam List<Long> ids){
        setmealService.delete(ids);
        return Result.success("删除成功");
    }
    @GetMapping("list")
    public Result<List<SetmealDto>> list(SetmealDto setmealDto){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmealDto.getId()!=null,Setmeal::getId,setmealDto.getId());
        queryWrapper.eq(setmealDto.getStatus()!=null,Setmeal::getStatus,setmealDto.getStatus());
        List<Setmeal> list = setmealService.list(queryWrapper);
        List<SetmealDto> dtoList=list.stream().map((item)->{
            SetmealDto dto = new SetmealDto();
            BeanUtils.copyProperties(item,dto);
            Long id = item.getId();
            LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SetmealDish::getSetmealId,id);
            List<SetmealDish> dishList = setmealDishService.list(wrapper);
            dto.setSetmealDishes(dishList);
            return dto;
        }).collect(Collectors.toList());
        return Result.success(dtoList);
    }
}
